//
//  SNSTextViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-8-4.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "SNSTextViewController.h"
#import "UIAlertView+Kirinrin.h"
#import "AppDelegate.h"
#import "NetworkAccessManager.h"

@interface SNSTextViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *contentView;

@end

@implementation SNSTextViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    //TODO:添加对于键盘弹出后TextView的处理
    [self.contentView becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)sendAction:(id)sender {
    
    NSString *textcontent = [_contentView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if ([textcontent isEqualToString:@""]) {
        [UIAlertView alertWithTitle:@"提示" message:@"请填写内容"];
        return;
    }
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSString *uriString = @"Messages.json";
    NSDictionary *parameters = @{@"key": appDelegate.accessKey,@"content": textcontent};
    
    [[NetworkAccessManager shareManager]POST:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
    
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            [self dismissViewControllerAnimated:YES completion:nil];
            
            if (self.delegate) {
                [self.delegate didSendSNSPost];
            }
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
}
- (IBAction)cancelAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
